///
/// AEye, Inc. ("COMPANY") CONFIDENTIAL
/// Unpublished Copyright (c) 2022 AEye, Inc., All Rights Reserved.
///
/// NOTICE: All information contained herein is, and remains the property of COMPANY. The intellectual and technical concepts contained
/// herein are proprietary to COMPANY and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.
/// Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained
/// from COMPANY. Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees, managers or contractors who have executed
/// Confidentiality and Non-disclosure agreements explicitly covering such access.
///
/// The copyright notice above does not evidence any actual or intended publication or disclosure of this source code, which includes
/// information that is confidential and/or proprietary, and is a trade secret, of COMPANY. ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE,
/// OR PUBLIC DISPLAY OF OR THROUGH USE OF THIS SOURCE CODE WITHOUT THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED, AND IN VIOLATION OF APPLICABLE
/// LAWS AND INTERNATIONAL TREATIES. THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY ANY RIGHTS
/// TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR IN PART.
///

#include <iostream>
#include "SensorListener.hpp"
#include "AEyeSDKConfig.hpp"

#include <thread>
#include <chrono>

#ifdef _MSC_VER
#define _WINSOCKAPI_
#include <Windows.h>
#include <WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")
#endif

/*
 * The main routine takes in the following input arguments:
 *   1. IP address of the AE110 sensor, and
 *   2. GIUD of the network interface used to connect with the sensor
 *   3. Optionally, the number of frames to capture. Defaults to 5.
 */

void printUsage() {
  printf("aeyeSensorListener - An example for using AEye's Sensor SDK\n\n");
  printf("Usage:\n");
  printf("  ./aeyeSensorListener <sensor_ip> <network_id> <number_of_frames>\n\n");
  printf("Example:\n");
#ifdef _MSC_VER
  printf("  .\\aeyeSensorListener.exe 10.10.10.198 \"Realtek USB GbE Family Controller\" 100\n\n");
#else
  printf("  ./aeyeSensorListener 10.10.10.198 eth0 100\n\n");
#endif
}

int main(int argc, char *argv[]) {
  aeye::InitializeLoggerStderrOnly();

  std::unique_ptr<SensorListener> sensorListener(new SensorListener());
  
  // Get the command line arguments
  std::string sensorIP;
  if (argc > 1) {
    sensorIP = argv[1];
  }
  else {
	printf("Missing sensor IP.\n\n");
	printUsage();
	return 0;
  }
  
  std::string networkInterface;
  if (argc > 2) {
	  networkInterface = argv[2];
  }
  else {
	printf("Missing network interface GUID.\n\n");
	printUsage();
	return 0;
  }
  
  uint16_t numFramesToCapture = 5;
  if (argc > 3) {
	  numFramesToCapture = std::strtoul(argv[3], nullptr, 0);
  }
  
  // Starting up WinSock
#ifdef _MSC_VER
  WSADATA wsaData;
  if (WSAStartup(MAKEWORD(1, 1), &wsaData) == SOCKET_ERROR) {
    printf("Error initialising WSA.\n");
    return -1;
  }
#endif

  // Add a sensor using the provided details
  sensorListener->addSensor(networkInterface, sensorIP);
  
  // Also, set the number of frames to capture
  sensorListener->numFramesToCapture = numFramesToCapture;


  // Start the LiDAR stream
  // NOTE: The laser WILL start firing at this point, please be careful
  //       and wear appropriate eye safety equipment when interacting
  //       with lasers.
  sensorListener->startSensors();

  std::cout << "Press any key to stop the demo" << std::endl;
  getchar();
  getchar();
  //std::this_thread::sleep_for (std::chrono::seconds(10));

  // Stop the LiDAR stream.
  // Now, the laser is not firing.
  sensorListener->stopSensors();

  // Disconnect the AE110 sensor from the system
  sensorListener->disconnectFromSensors();
  // Cleans up WinSock from system
#ifdef _MSC_VER
  WSACleanup();
#endif
}
